#include "StdAfx.h"
#include "UrlHelp.h"
#include <algorithm>


#define URL_HTTP    "HTTP://"
#define URL_HTTPS   "HTTPS://"
#define URL_FILE    "FILE://"
#define RUL_Torrent "TORRENT://"


CUrlHelp::CUrlHelp(void)
{
	m_dsType = DS_Unknow;
}

CUrlHelp::~CUrlHelp(void)
{

}

void CUrlHelp::SetContext(LPCSTR szUrl )
{
	m_strContext = szUrl;
	std::transform(m_strContext.begin(), m_strContext.end(), m_strContext.begin(),  toupper);
	hasher helper(m_strContext.c_str() , m_strContext.size());
	
	do
	{
		if (m_strContext.find(URL_HTTP , 0 , strlen(URL_HTTP)) != std::string::npos)
		{
			m_dsType = DS_Http;
			break;
		}
		
		if (m_strContext.find(URL_HTTPS , 0 , strlen(URL_HTTPS)) != std::string::npos)
		{
			m_dsType = DS_Http;
			break;
		}

		if (m_strContext.find(URL_FILE , 0 , strlen(URL_FILE)) != std::string::npos)
		{
			m_dsType = DS_File;
			break;
		}

		if (m_strContext.find(RUL_Torrent , 0 , strlen(RUL_Torrent)) != std::string::npos)
		{
			m_dsType = DS_Torrent;
			break;
		}
		

	}while (FALSE);
}

sha1_hash CUrlHelp::GetHash()
{
	if (m_strContext.empty())
	{
		return NullUrlHash;
	}
	
	hasher  helper(m_strContext.c_str() , m_strContext.size());
	return helper.final();
}


DSType CUrlHelp::GetDownLoadType()
{
	return m_dsType;
}